package com.wcity.manage.service.community.workguide.impl;

import com.wcity.manage.common.enums.SaveImageType;
import com.wcity.manage.common.vo.community.workguide.WorkGuideVo;
import com.wcity.manage.dao.mapper.community.workguide.WorkGuideMapper;
import com.wcity.manage.dao.model.community.image.ImageUrl;
import com.wcity.manage.dao.model.community.workguide.WorkGuide;
import com.wcity.manage.service.community.image.ImageService;
import com.wcity.manage.service.community.workguide.WorkGuideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 办事指南
 * Created by hgg on 2016/12/20.
 */
@Service
public class WorkGuideServiceImpl implements WorkGuideService {

    @Autowired
    private WorkGuideMapper workGuideMapper;
    @Autowired
    private ImageService imageService;

    /**
     * 办事指南列表
     * @param queryMap
     * @return
     */
    @Override
    public List<WorkGuideVo> workGuidePageList( Map<String, Object> queryMap ) {

        return workGuideMapper.workGuidePageList( queryMap );
    }

    /**
     * 删除办事指南
     * @param deleteMap
     * @return
     */
    @Override
    public Integer deleteWorkGuide( Map<String, Object> deleteMap ) {

        return workGuideMapper.deleteWorkGuide( deleteMap );
    }

    /**
     * 新增办事指南
     * @param workGuide
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer saveAddWorkGuide( WorkGuide workGuide,String blockIdStr,String imgUrl ) {

        //【1】生成父级办事指南
        Integer parentId = 0;
        String[] blockIds = blockIdStr.split( "," );
        workGuide.setBlockId( Integer.parseInt( blockIds[0] ) );
        workGuide.setPid( 0 );
        Integer saveNum = workGuideMapper.saveAddWorkGuide( workGuide );
        if( saveNum == 0 ){
            throw new RuntimeException("新增办事指南失败");
        }
        parentId = workGuide.getId();

        //【2】保存关联封面图片
        ImageUrl image = new ImageUrl( null,imgUrl,null,parentId,SaveImageType.COMMUNITY_WORK_GUIDE );
        Integer saveImageNum = imageService.saveAddImage( image );
        if( saveImageNum == 0 ){
            throw new RuntimeException( "保存小区办事指南封面图片失败" );
        }

        //【3】执行保存小区办事指南
        saveAddWorkGuide( workGuide,blockIds,parentId,imgUrl );

        return 1;
    }

    /**
     * 更新办事指南
     * @param workGuide
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer saveUpdateWorkGuide( WorkGuide workGuide,String blockIdStr,String imageUrl ) {


        Integer parentId = workGuide.getId();
        String[] blockIds = blockIdStr.split( "," );
        //【1】删除以前办事指南封面
        Map<String,Object> deleteMap = new HashMap<>();
        deleteMap.put( "referId", workGuide.getId() );
        deleteMap.put( "saveType", SaveImageType.COMMUNITY_WORK_GUIDE);
        imageService.deleteImage( deleteMap );

        //【2】删除以前的小区概况
//        Map<String,Object> deleteWorkGuideMap = new HashMap<>();
//        deleteWorkGuideMap.put( "parentId",workGuide.getId() );
//        deleteWorkGuideMap.put( "userId",workGuide.getUserId() );
//        Integer deleteNum = workGuideMapper.deleteWorkGuide( deleteWorkGuideMap );
//        if( deleteNum == 0 ){
//            throw new RuntimeException( "删除小区办事指南失败" );
//        }

        //【3】执行保存办事指南封面
        ImageUrl image = new ImageUrl( null,imageUrl,null,workGuide.getId(),SaveImageType.COMMUNITY_WORK_GUIDE );
        Integer saveImageNum = imageService.saveAddImage( image );
        if( saveImageNum == 0 ){
            throw new RuntimeException( "保存小区办事指南封面图片失败" );
        }

        //【4】执行更新办事指南概况
        workGuide.setBlockId( Integer.parseInt( blockIds[0] ) );
        Integer updateNum = workGuideMapper.saveUpdateWorkGuide( workGuide );
        if( updateNum == 0 ){
            throw new RuntimeException( "更新社区概况失败 ");
        }

        //【5】执行新增小区办事指南概况
//        saveAddWorkGuide( workGuide, blockIds, parentId,imageUrl );
        return 1;
    }

    /**
     * 办事指南详情
     * @param detailMap
     * @return
     */
    @Override
    public WorkGuideVo workGuideDetail( Map<String, Object> detailMap ) {

        return workGuideMapper.workGuideDetail( detailMap );
    }

    /**
     * 办事指南总数量
     * @param queryMap
     * @return
     */
    @Override
    public Integer totalWorkGuide( Map<String, Object> queryMap ) {

        return workGuideMapper.totalWorkGuide( queryMap );
    }

    /**
     * 办事指南列表非分页
     * @param queryMap
     * @return
     */
    @Override
    public List<WorkGuideVo> workGuideList( Map<String, Object> queryMap ) {

        return workGuideMapper.workGuideList( queryMap );
    }

    /**
     * 执行保存小区办事指南
     * @param workGuide
     * @param blockIds
     * @param parentId
     */
    private void saveAddWorkGuide( WorkGuide workGuide,String[] blockIds,Integer parentId,String imgUrl ){

        for ( String id : blockIds ){
            workGuide.setBlockId( Integer.parseInt( id ) );
            //【1】执行保存
            workGuide.setPid( parentId );
            Integer saveNum = workGuideMapper.saveAddWorkGuide( workGuide );
            if( saveNum == 0 ){
                throw new RuntimeException("保存小区办事指南失败");
            }

            //【2】保存关联封面图片
            ImageUrl image = new ImageUrl( null,imgUrl,null,workGuide.getId(),SaveImageType.COMMUNITY_WORK_GUIDE );
            Integer saveImageNum = imageService.saveAddImage( image );
            if( saveImageNum == 0 ){
                throw new RuntimeException( "保存小区办事指南封面图片失败" );
            }
        }
    }

    /**
     * 查询小区ID拼接（1,2,3）
     * @param queryMap
     * @return
     */
    public String blockIds( Map<String,Object> queryMap ){

        return workGuideMapper.blockIds( queryMap );
    }
}
